﻿using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

public class SessionService
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public SessionService(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public bool IsLogin()
    {
        var session = _httpContextAccessor.HttpContext?.Session;
        if (session == null)
        {
            return false;
        }

        string? username = session.GetString("user.username");
        return !string.IsNullOrEmpty(username);
    }

    public int GetUserId()
    {
        return GetInt32("user.id");
    }
    
    public string GetString(string key)
    {
        var session = _httpContextAccessor.HttpContext?.Session;
        if (session == null)
        {
            return "";
        }
        return session.GetString(key) ?? "";
    }
    
    public int GetInt32(string key)
    {
        var session = _httpContextAccessor.HttpContext?.Session;
        if (session == null)
        {
            return 0;
        }
        return session.GetInt32(key) ?? 0;
    }
}